package com.bonc.jedisCluster.test;

import java.util.HashSet;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import com.bonc.jedisCluster.utils.JedisClusterExecutor;

import redis.clients.jedis.HostAndPort;

public class SetnxExample {
	public static void main(String[] args) {
		HashSet<HostAndPort> hostAndPort = new HashSet<HostAndPort>();
		hostAndPort.add(new HostAndPort("192.168.1.138", 6379));
		hostAndPort.add(new HostAndPort("192.168.1.138", 6380));
		JedisClusterExecutor jedisCluster = JedisClusterExecutor.getInstance(hostAndPort, 5000, 5000, 5, new GenericObjectPoolConfig());
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				while(true) {
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					String lockVal = jedisCluster.lock("lock", 10);
					if (lockVal==null) {
						continue;
					}
					// 获得锁期间所做的事情
					System.out.println(lockVal);
					jedisCluster.unlock("lock", lockVal);
					break;
				}
			}
		}).start();
		String lockVal = jedisCluster.lock("lock", 10);
		if (lockVal!=null) {
			System.out.println(lockVal);
			System.out.println(1);
			jedisCluster.unlock("lock", lockVal);
		}
	}
}
